/**************************************************************************************
*  jdbg.h
*
*
*
**************************************************************************************
*
*  The contents of this file are subject to the RTJ COMPUTING non-commercial
*  source code license agreement ("License"); you may not use this file except in
*  compliance with the License. A copy of the License (license.terms) can be
*  found in the root of installation folder or on the web at
*  http://www.rtjcom.com/Files/license.terms
*
*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
*  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
*  MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
*  IN NO EVENT SHALL RTJ COMPUTING BE LIABLE FOR ANY CLAIM, DAMAGES
*  OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
*  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
*  OTHER DEALINGS IN THE SOFTWARE.
*
*  Copyright (c) 2000-2002 RTJ Computing Pty. Ltd. All rights reserved.
*
**************************************************************************************/

#ifndef _JDBG_H
#define _JDBG_H

#include "jvm.h"

#if ENABLE_REMDBG

#define DBG_VERSION         0x0101  /* debugger interface version number */

/* commands initiated by remote debugger */
#define DREQ_SYNC           0x10
#define DRESP_SYNC          0x11
#define DREQ_GETMEMSTAT     0x18
#define DRESP_GETMEMSTAT    0x19
#define DREQ_SETBREAK       0x20
#define DRESP_SETBREAK      0x21
#define DREQ_RUN            0x28
#define DRESP_RUN           0x29
#define DREQ_STEPINTO       0x30
#define DRESP_STEPINTO      0x31
#define DREQ_STEPOVER       0x38
#define DRESP_STEPOVER      0x39
#define DREQ_STEPOUT        0x40
#define DRESP_STEPOUT       0x41
#define DREQ_INSPECT        0x48
#define DRESP_INSPECT       0x49
#define DREQ_GETARRAY       0x50
#define DRESP_GETARRAY      0x51
#define DREQ_ABORT          0x58
#define DRESP_ABORT         0x59
#define DREQ_RESTART        0x60
#define DRESP_RESTART       0x61
#define DREQ_GETTHREADS     0x68
#define DRESP_GETTHREADS    0x69
#define DREQ_GETSTACK       0x70
#define DRESP_GETSTACK      0x71
#define DREQ_SETVALUE       0x78
#define DRESP_SETVALUE      0x79
#define DREQ_PAUSE          0x80    /* no response sent for PAUSE */
#define DREQ_SETPARAMS      0x88
#define DRESP_SETPARAMS     0x89
#define DREQ_SETDBGTHREAD   0x90
#define DRESP_SETDBGTHREAD  0x91
#define DREQ_SETSTRING      0x92
#define DRESP_SETSTRING     0x93


/* commands initiated by the VM */
#define DREQ_PCPOS          0xF8
#define DRESP_PCPOS         0xF9
#define DREQ_PROGEND        0xFA


#define CMD_GET             true
#define CMD_PUT             false

#define MAX_REQ_SIZE        64
#define MAX_RESP_SIZE       256
#define MAX_BREAKS          16

enum { EV_NONE, EV_EXCEP, EV_BREAK, EV_BACKJUMP, EV_STEPOUT, EV_STEPINTO, EV_PAUSE };
enum { BRK_CLEAR, BRK_SET, BRK_CLEAR_ALL };
enum { DMOD_NONE, DMOD_RUN, DMOD_RUNTO, DMOD_STEPOVER, DMOD_STEPINTO, DMOD_STEPOUT };
enum { IT_NONE, IT_LOCAL, IT_FIELD, IT_STATIC, IT_THIS, IT_ARRAY };

#if ENABLE_PRAGMA_PACK
#pragma pack(2)
#endif

typedef struct
{
    uint16 len;
    uint16 cmd;
} cmd_t;

typedef struct
{
    uint16 len;
    uint16 cmd;
    uint32 heap;
    uint16 refs;
    uint16 strings;
    uint16 threads;
    uint16 breaks;
    uint32 app_start;
    uint16 time_slice;
    uint16 timers;
    uint16 events;
    uint8 app_name[16];
    uint16 unicode;
    uint16 cl_ver;
    uint16 rxbuf_size;
    uint16 dbg_ver;
} resp_synch_t;

typedef struct
{
    uint16 len;
    uint16 cmd;
    uint32 heap;
    uint16 refs;
    uint16 strings;
    uint16 threads;
} resp_memstat_t;

typedef struct
{
    uint16 len;
    uint16 cmd;
    uint32 pc;
    uint32 pass_count;
    uint16 thread;
    uint16 set;             /* 0=clear, 1=set, 2=clear all */
} cmd_break_t;

typedef struct
{
    uint16 len;
    uint16 cmd;
    uint16 status;          /* 1=set/clear success, 0=set fail - no more room in table */
} resp_break_t;

typedef struct
{
    uint32 pc;              /* absolute memory address of a breakpoint */
    uint16 thread;          /* thread id, 0xFFFF=apply to all threads */
    uint32 pass_count;      /* number of passes before execution is stopped, 0=stop always */
    uint32 count;           /* passes counter */
} break_t;

typedef struct
{
    uint16 id;              /* thread ID number */
    uint32 cls;             /* address of class associated with this thread */
    uint16 status;          /* thread status, running, waiting,... */
    uint32 pc;              /* current frame absolute PC position */
} thread_entry_t;

typedef struct
{
    uint16 len;
    uint16 cmd;
    uint16 count;
    thread_entry_t threads[1];
} resp_getthreads_t;

typedef struct
{
    uint16 len;
    uint16 cmd;
    uint32 value;
} resp_inspect_t;

typedef struct
{
    uint16 len;
    uint16 cmd;
    uint32 arrlen;
    uint32 count;
    uint16 data[1];
} resp_getarray_t;

typedef struct
{
    uint16 len;
    uint16 cmd;
    uint16 thread_id;       /* thread ID for which to return the stack trace, 0=current thread */
} cmd_getstack_t;

typedef struct
{
    uint32 method;
    uint32 pc;
} stack_entry_t;

typedef struct
{
    uint16 len;
    uint16 cmd;
    uint16 count;
    stack_entry_t stack[1];
} resp_getstack_t;

typedef struct
{
    uint16 len;
    uint16 cmd;
    uint16 excep_break;
    uint32 runto_break;
} cmd_run_t;

typedef struct
{
    uint16 len;
    uint16 cmd;
    uint32 next_pc;
    uint16 excep_break;
} cmd_step_t;

typedef struct
{
    uint16 len;
    uint16 cmd;
    uint16 excep_break;
} cmd_stepout_t;

typedef struct
{
    uint16 len;
    uint16 cmd;
    uint32 pc;
    uint16 event;
    uint32 excep_pc;
} cmd_pcpos_t;

typedef struct
{
    uint16 len;
    uint16 cmd;
    uint16 reason;
} cmd_progend_t;

typedef struct
{
    uint16 len;
    uint16 cmd;
    uint16 type;
    uint32 object;
    uint16 slot;
} cmd_inspect_t;

typedef struct
{
    uint16 len;
    uint16 cmd;
    uint32 count;
    uint32 index;
    uint32 object;
} cmd_getarray_t;

typedef struct
{
    uint16 len;
    uint16 cmd;
    uint16 type;
    uint32 object;
    uint16 slot;
    uint32 value;
} cmd_setvalue_t;

typedef struct
{
    uint16 len;
    uint16 cmd;
    uint16 arg_count;
    uint32 args[1];
} cmd_setparams_t;

typedef struct
{
    uint16 len;
    uint16 cmd;
    uint16 thread;
} cmd_setdbgthread_t;

typedef struct
{
    uint16 len;
    uint16 cmd;
    uint32 object;
    uint16 offset;
    uint16 count;
    uint8 data[1];
} cmd_setstring_t;

#if ENABLE_PRAGMA_PACK
#pragma pack()
#endif

extern bool abortDbg;
extern bool restartDbg;
extern uint16 dbg_args_count;
extern int8 **dbg_args;
extern bool dbg_appRunning;
extern uint32 dbg_excepPC;
extern uint32 dbg_catchPC;

void initDbg(void);
void dbgAbort(void);
void processDbgCommands(bool wait);
void pauseRun(void);
void cmdProgEnd(int16 excep);

#endif

#endif

